<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>批处理程序</title>
    <style>
        ul {
            margin: 0;
            padding: 0;
            list-style-type: none;
        }

        #command {
            width: 200px;
            height: 200px;
        }
    </style>
</head>

<body>

    <div class="batchApp">
        <div>
            <div class="content">
                <table>
                    <tbody>
                        <tr>
                            <p>请输入命令:</p>
                            <td>
                                <textarea id="command" placeholder="命令格式按照cmd的格式,且必须要有两行命令以上!"></textarea>
                            </td>
                            <td>
                                <button onclick="openCmdTest()">执行cmd</button>
                                <button onclick="saveCmdCommand()">保存cmd命令</button>

                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
            <div class="aside">
                <ul>
                    <li></li>
                </ul>
            </div>
        </div>
    </div>
</body>

<script type="module">
    const { cmdApi } = require('./renderer.js')
    const fs = require('fs')
    let cmdCommand;
    let commandArray;
    /*
    * 更新输入框(textarea)的值
    *为输入框绑定一个事件
    */
    function getValue() {

        document.getElementById('command').onkeypress = function () {

            cmdCommand = document.getElementById('command').value
            commandArray = cmdCommand.split(/[\n]/)
        }
        console.log(commandArray,'aaaaaa')
        return { commandArray }

    }


    // 打开cmd进行测试
    function openCmdTest() {

        // 执行cmd命令
        cmdApi.execCmd(commandArray)

    }

    function saveCmdCommand() {

        const commandArray = getValue()

        /*
         *保存下来cmd命令
         */
        fs.readFile("assets/txt/command.json", (err, data) => {
            // if (err) {
            //     // return console.log(err);
            // }
            console.log('sdf');
            for (let i = 0; i < 3; i++) {
                console.log(commandArray[i], 'iiiiiiii')        
                // fs.writeFile('assets/txt/command.json', commandArray[i], function (err) {
                //     if (err) {
                //         console.log(err);
                //     }
                //     console.log('陈宫')
                // })
            }

        })

        // 遍历cmd数组并保存到command.txt



    }

    // 全局对象中的自定义方法
    window.openCmdTest = openCmdTest
    window.saveCmdCommand = saveCmdCommand
    window.getValue = getValue

    // 全局对象中的自定义变量
    window.cmdCommand = cmdCommand
    window.commandArray = commandArray

</script>
<script>


</script>

</html>